package com.atm.thirdplt.service.impl.view;

import com.atm.thirdplt.common.annotation.DataSource;
import com.atm.thirdplt.common.core.domain.his.*;
import com.atm.thirdplt.common.core.domain.lis.LisLabResult;
import com.atm.thirdplt.common.core.domain.lis.LisLabTestMaster;
import com.atm.thirdplt.common.enums.DataSourceType;
import com.atm.thirdplt.dao.mapper.his.HisInfMapper;
import com.atm.thirdplt.dao.mapper.lis.LisInfMapper;
import com.atm.thirdplt.service.IHisInfQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库视图方式查询His/lis数据 实现类
 *
 * @author atm
 */
@Service("viewQuery")
public class ViewQueryServiceImpl implements IHisInfQueryService {

    private static final Logger log = LoggerFactory.getLogger(ViewQueryServiceImpl.class);

    @Autowired
    private HisInfMapper hisInfMapper;

    @Autowired
    private LisInfMapper lisInfMapper;

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisDeptDict> selDeptDic() {
        return hisInfMapper.selDeptDic();
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisUsers> selHisUsers() {
        return hisInfMapper.selHisUsers();
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisOperationSchedule> selHisOpertSchele(String patId, String visitId, String startTime,
                                                        String endTime) {
        return hisInfMapper.selHisOpertSchedule(patId, visitId, startTime, endTime);
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisScheduledOperationName> selScheOperNameHis(String patId, String visitId, String scheduleId) {
        return hisInfMapper.getScheOperName(patId, visitId, scheduleId);
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisPatsInHospital> selPatInHosHis(List<String> patIdList) {
        return hisInfMapper.getPatInHos(patIdList);
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisPatMasterIndex> selPatInfHis(List<String> patIdList) {
        return hisInfMapper.selPatInfByIds(patIdList);
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisPatMasterIndex> selPatInfByConHis(String patId) {
        return hisInfMapper.selPatInfByConHis(patId);
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisOperationDict> selOperDicHis() {
        return hisInfMapper.selOperDicHis();
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisPatsInHospital> selPatInHosByConHis(String patId, String inpNo) {
        List<HisPatsInHospital> newList = new ArrayList<>();
        List<HisPatsInHospital> queryList = hisInfMapper.selPatInHosByConHis(patId, inpNo);
        if (!CollectionUtils.isEmpty(queryList)) {
            newList.add(queryList.get(0));
        }
        return newList;
    }

    @Override
    @DataSource(DataSourceType.LIS)
    public List<LisLabTestMaster> selLabMaster(String patId, String inpNo, String visitId) {
        //用病人住院号代替病人ID去查询检验信息
        return lisInfMapper.selLabMaster(inpNo);
    }

    @Override
    @DataSource(DataSourceType.LIS)
    public List<LisLabResult> selLabResult(List<String> testNo) {
        return lisInfMapper.selLabResult(testNo);
    }

    @Override
    @DataSource(DataSourceType.HIS)
    public List<HisPatMasterIndex> selPatInfByClinicNumber(String clinicNumber) {
        return hisInfMapper.selPatInfByClinicNumber(clinicNumber);
    }
}
